<?php
class ModelNewsArticle extends Model {
	public function addArticle($data) {
		$restaurant_id = $this->restaurant->getRestaurantId() == null? '0' : $this->restaurant->getRestaurantId();
		
		$sql  = " INSERT INTO " . DB_PREFIX . "news_article ";
		$sql .= " SET article_id 	= '" . (int)$data['article_id'] . "',";
		$sql .= "	category_id 		= '" . (int)$data['category_id'] . "',";
		$sql .= "	sort_order 		= '" . (int)$data['sort_order'] . "',";		
		$sql .= "	restaurant_id   = '" . (int)$restaurant_id . "',";
		$sql .= "	`status` 	    = '" . (int)$data['status'] . "',";
		$sql .= "	date_added 		= NOW()";
		
		$this->db->query($sql);
		
		$article_id = $this->db->getLastId();
	
		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "news_article SET image = '" . $this->db->escape($data['image']) . "' WHERE article_id = '" . (int)$article_id . "'");
		}
		
		foreach ($data['news_article_description'] as $language_id => $value) {
			$sql  = " INSERT INTO " . DB_PREFIX . "news_article_description ";
			$sql .= " SET article_id 		= '" . (int)$article_id . "'";
			$sql .= " , language_id 		= '" . (int)$language_id . "'";
			$sql .= " , title 				= '" . $this->db->escape($value['title']) . "'";
			$sql .= " , header				= '" . $this->db->escape($value['header']) . "'";
			$sql .= " , body 				= '" . $this->db->escape($value['body']) . "'";
			$sql .= " , meta_keyword 		= '" . $this->db->escape($value['meta_keyword']) . "'";
			$sql .= " , meta_description 	= '" . $this->db->escape($value['meta_description']) ."'";
			$this->db->query($sql);
		}

		if ($data['keyword']) {
			$sql  = " INSERT INTO " . DB_PREFIX . "url_alias ";
			$sql .= " SET query = 'article_id=" . (int)$article_id . "'";
			$sql .= " , keyword = '" . $this->db->escape($data['keyword']) . "'";
			$this->db->query($sql);
		}
		
		$this->cache->delete('news');
	}

	public function editArticle($article_id, $data) {
		$restaurant_id = $this->restaurant->getRestaurantId() == null? '0' : $this->restaurant->getRestaurantId();
		
		$sql  = "UPDATE " . DB_PREFIX . "news_article SET ";
		$sql .= "	category_id 		= '" . (int)$data['category_id'] . "',";
		$sql .= "	sort_order 		= '" . (int)$data['sort_order'] . "',";
		$sql .= "	date_modified 	= NOW() ";
		
		if(isset($data['status'])) {
			$sql .= " , `status` = '" . (int)$data['status'] . "'";
		}
		
		$sql .= " WHERE article_id = '" . (int)$article_id . "'";		
		
		$this->db->query($sql);
		
		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "news_article SET image = '" . $this->db->escape($data['image']) . "' WHERE article_id = '" . (int)$article_id . "'");
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "news_article_description WHERE article_id = '" . (int)$article_id . "'");
		
		foreach ($data['news_article_description'] as $language_id => $value) {
			$sql  = " INSERT INTO " . DB_PREFIX . "news_article_description ";
			$sql .= " SET article_id 		= '" . (int)$article_id . "'";
			$sql .= " , language_id 		= '" . (int)$language_id . "'";
			$sql .= " , title 			= '" . $this->db->escape($value['title']) . "'";
			$sql .= " , header			= '" . $this->db->escape($value['header']) . "'";
			$sql .= " , body 				= '" . $this->db->escape($value['body']) . "'";
			$sql .= " , meta_keyword 		= '" . $this->db->escape($value['meta_keyword']) . "'";
			$sql .= " , meta_description 	= '" . $this->db->escape($value['meta_description']) ."'";
			$this->db->query($sql);
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'article_id=" . (int)$article_id. "'");
		if ($data['keyword']) {
			$sql  = " INSERT INTO " . DB_PREFIX . "url_alias ";
			$sql .= " SET query = 'article_id=" . (int)$article_id . "'";
			$sql .= " , keyword = '" . $this->db->escape($data['keyword']) . "'";
			$this->db->query($sql);
		}
		
		$this->cache->delete('news');
	}

	public function deleteArticle($article_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "news_article WHERE article_id = '" . (int)$article_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "news_article_description WHERE article_id = '" . (int)$article_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'article_id=" . (int)$article_id . "'");
		
		$this->cache->delete('news');
	}

	public function getArticle($article_id) {
		$restaurant_id = $this->restaurant->getRestaurantId() == null? '0' : $this->restaurant->getRestaurantId();
		$sql  = " SELECT rna.article_id, rna.category_id, rna.image, rna.sort_order, rna.`status`, rna.date_modified AS date,";
		$sql .= " 	(SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'article_id=" . (int)$article_id . "') AS keyword";
		$sql .= " FROM " . DB_PREFIX . "news_article rna";
		$sql .= " WHERE rna.restaurant_id = " . (int)$restaurant_id ;
		$sql .= " 	AND rna.article_id = " . (int)$article_id;
		$query = $this->db->query($sql);
		return $query->row;
	}

	public function getArticles($data = array()) {
		$restaurant_id = $this->restaurant->getRestaurantId() == null? '0' : $this->restaurant->getRestaurantId();
		
		$sql  = " SELECT rna.article_id, rna.image, rna.`status`, rna.date_added, rna.date_modified AS date, rnad.title,";
		$sql .= "	rncd.name as 'category_name'";
		$sql .= " 	FROM " . DB_PREFIX . "news_article rna ";
		$sql .= " 	LEFT JOIN " . DB_PREFIX . "news_article_description rnad ON (rna.article_id = rnad.article_id)";
		$sql .= " 	LEFT JOIN " . DB_PREFIX . "news_category rnc ON (rna.category_id = rnc.category_id)";
		$sql .= " 	LEFT JOIN " . DB_PREFIX . "news_category_description rncd ON (rnc.category_id = rncd.category_id)";
		$sql .= " WHERE	rnad.language_id = '" . (int)$this->config->get('config_language_id') . "'";
		$sql .= "	AND rncd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
		$sql .= "	AND rna.restaurant_id = '" . (int)$restaurant_id . "'";
		
		if (isset($data['filter_title']) && !is_null($data['filter_title'])) {
			$sql .= " AND LCASE(rnad.title) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_title'])) . "%'";
		}
		
		if (isset($data['filter_category']) && !is_null($data['filter_category'])) {
			$sql .= " AND rnatc.category_id = '" . (int)$data['filter_category'] . "'";
		}
		
		if (isset($data['filter_type']) && !is_null($data['filter_type']) && $data['filter_type'] != '*') {
			$sql .= " AND rnc.type_id = '" . (int)$data['filter_type'] . "'";
		}
		
		if (isset($data['filter_category']) && !is_null($data['filter_category'])) {
			$sql .= " AND rnatc.category_id = '" . (int)$data['filter_category'] . "'";
		}
		
		if (isset($data['filter_month']) && !is_null($data['filter_month'])) {
			$sql .= " AND (DATE_FORMAT(rna.date_modified,'%c/%Y') = '" . $data['filter_month'] . "'";
			$sql .= " OR  CONCAT('0', DATE_FORMAT(rna.date_modified,'%c/%Y')) = '" . $data['filter_month'] . "')";
		}
				
		if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
			$sql .= " AND rna.`status` = '" . (int)$data['filter_status'] . "'";
		}
		
		$sort_data = array('rnad.title', 'c.type_id', 'rna.date_added', 'rna.sort_order', 'rncd.name');	
		
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY rna.date_added";	
		}
		
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}				

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
		
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
		
		$query = $this->db->query($sql);
		
		return $query->rows;
	}

	public function getTotalArticle($data = array()){
		$restaurant_id = $this->restaurant->getRestaurantId() == null? '0' : $this->restaurant->getRestaurantId();
		
		$sql  = " SELECT COUNT(DISTINCT rna.article_id) AS total ";
		$sql .= " 	FROM " . DB_PREFIX . "news_article rna ";
		$sql .= " 	LEFT JOIN " . DB_PREFIX . "news_article_description rnad ON (rna.article_id = rnad.article_id)";
		$sql .= " 	LEFT JOIN " . DB_PREFIX . "news_category rnc ON (rna.category_id = rnc.category_id)";
		$sql .= " 	LEFT JOIN " . DB_PREFIX . "news_category_description rncd ON (rnc.category_id = rncd.category_id)";
		$sql .= " WHERE	rnad.language_id = '" . (int)$this->config->get('config_language_id') . "'";
		$sql .= "	AND rncd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
		$sql .= "	AND rna.restaurant_id = '" . (int)$restaurant_id . "'";
		
		if (isset($data['filter_title']) && !is_null($data['filter_title'])) {
			$sql .= " AND LCASE(rnad.title) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_title'])) . "%'";
		}
		
		if (isset($data['filter_category']) && !is_null($data['filter_category'])) {
			$sql .= " AND rnatc.category_id = '" . (int)$data['filter_category'] . "'";
		}
	
		if (isset($data['filter_type']) && !is_null($data['filter_type']) && $data['filter_type'] != '*') {
			$sql .= " AND rnc.type_id = '" . (int)$data['filter_type'] . "'";
		}
		
		if (isset($data['filter_category']) && !is_null($data['filter_category'])) {
			$sql .= " AND rnatc.category_id = '" . (int)$data['filter_category'] . "'";
		}
		
		if (isset($data['filter_month']) && !is_null($data['filter_month'])) {
			$sql .= " AND (DATE_FORMAT(rna.date_modified,'%c/%Y') = '" . $data['filter_month'] . "'";
			$sql .= " OR  CONCAT('0', DATE_FORMAT(rna.date_modified,'%c/%Y')) = '" . $data['filter_month'] . "')";
		}
		
		if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
			$sql .= " AND rna.`status` = '" . (int)$data['filter_status'] . "'";
		}

		$query = $this->db->query($sql);
		
		return $query->row['total'];
	}
	 
	public function getArticleDescriptions($article_id) {
		$news_article_description_data = array();
		$sql = "SELECT * FROM " . DB_PREFIX . "news_article_description WHERE article_id = '" . (int)$article_id . "'";
		$query = $this->db->query($sql);
		foreach ($query->rows as $result) {
			$news_article_description_data[$result['language_id']] = array(
				'title'             => $result['title'],
				'header'            => $result['header'],
				'body'             	=> $result['body'],
				'meta_keyword'     	=> $result['meta_keyword'],
				'meta_description'	=> $result['meta_description']
			);
		}
		return $news_article_description_data;
	}
	
	public function getAutocompleteCategoryName($filter_name){
		$restaurant_id = $this->restaurant->getRestaurantId() == null? '0' : $this->restaurant->getRestaurantId();
		$sql   = " SELECT rncd.category_id, rncd.`name` AS category_name";
		$sql  .= " FROM ". DB_PREFIX . "news_category_description rncd ";
		$sql  .= " INNER JOIN ". DB_PREFIX . "news_category_to_restaurant rncts ON rncd.category_id = rncts.category_id";
		$sql  .= " WHERE rncts.restaurant_id = '". (int)$restaurant_id ."'"; 
		$sql  .= " AND rncd.language_id = '". (int)$this->config->get('config_language_id') ."'";
		$sql  .= " AND LCASE(rncd.`name`) LIKE '" . $this->db->escape(utf8_strtolower($filter_name)) . "%'";
		$query = $this->db->query($sql);

		return $query->rows;
	}
}
?>